﻿module Program
open System
open System.Collections.Generic
open System.IO
open MathNet.Numerics
open MathNet.Numerics.FSharp
open MathNet.Numerics.LinearAlgebra.Double
open KMean
open System.Drawing
open System.Windows.Forms
open Types


//lettura dei dati da file
let stream = File.Open("..\..\data\semeion.data",FileMode.Open, FileAccess.Read)
let stream_reader = new StreamReader(stream) 
let text = stream_reader.ReadToEnd()
stream_reader.Close()
stream.Close()


let labels = Map.ofList([("Iris-setosa",0.); ("Iris-versicolor",1.); ("Iris-virginica",2.)])

let input =

    let splitted_text = text.Split('\n')  
                        |> Seq.filter(fun elem -> elem <> "") 
                        |> Seq.toList

    let rec build_vectors (list : (int*string) list) =
      match list with
      []  -> []
      |(i,x)::xs -> 
        let first_266 = x.Split(' ', '\r')  
                            |> Seq.filter(fun elem -> elem <> "") 
                            |> Seq.map(fun p -> float p) 
                            
        let first_256 = first_266 |> Seq.take 256 |> Seq.toArray
        let real_number   = first_266 |> Seq.skip 256 |> Seq.mapi(fun i p -> if float p = 1.0 then i else 0) |> Seq.sum


        let points = new DenseVector(first_256)

        DenseVectorWrapper(points, i, real_number, -1 |> ref) :: build_vectors xs
        
    

    List(build_vectors (splitted_text |> List.mapi(fun i s -> (i,s))) |> Seq.ofList)

let timer = new System.Diagnostics.Stopwatch()
timer.Start()
let res = k_mean 10 input
timer.Stop()
System.Console.WriteLine()
System.Console.WriteLine(timer.ElapsedMilliseconds / int64 1000)
System.Console.WriteLine()

System.Console.Write(snd res)
System.Threading.Thread.Sleep(10000)
()